package com.aiju.cash.ui.discount;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.aiju.app.SimpleHttpCallback;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.bean.Discount;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DiscountApi;
import com.aiju.cash.util.GsonHelper;
import com.aiju.cash.widgit.CenterImageSpan;
import com.aiju.cash.widgit.DiscountView;
import com.aiju.utils.PriceUtil;
import com.aiju.utils.T;

import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-05-10 10:38
 * @version: V1.0
 * @Description: 满减，满折
 */
public class DiscountFullActivity extends BaseTitleActivity {

    private DiscountApi discountApi;

    private DiscountView discountView;

    private String type;

    private ListView discountListView;

    private List<Discount> discounts;

    private TextView button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discount_full);
        String title = getIntent().getStringExtra("title");
        setCenterTitle(title);

        discountApi = new DiscountApi(mDialogHelper);
        discountView = (DiscountView) findViewById(R.id.discount_whole);
        if (title.equals("满减")) {
            type = "3";
            discountView.setTitle(title);
            discountView.setContent("开启后，您可以在顾客买单时提供“满减”的优惠");
            discountView.setLeftDrawable(R.mipmap.ic_manjian);
            discountView.getRightImageView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.setSelected(!v.isSelected());
                    updateSwitch();
                }
            });
        } else {//满折
            type = "4";
            discountView.setTitle(title);
            discountView.setContent("开启后，您可以在顾客买单时提供“满折”的优惠");
            discountView.setLeftDrawable(R.mipmap.ic_manzhe);
            discountView.getRightImageView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.setSelected(!v.isSelected());
                    updateSwitch();

                }
            });
        }
        Spannable content = new SpannableStringBuilder("占位  " + "新增规则");
        CenterImageSpan span = new CenterImageSpan(this, R.mipmap.ic_discont_add, ImageSpan.ALIGN_BASELINE);
        content.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        discountListView = (ListView) findViewById(R.id.lv_discount_full);
        button = (TextView) findViewById(R.id.btn_discount_add);
        button.setText(content);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (discounts.size() >= 5) {
                    if (type.equals("4")) {
                        T.ToastShort("最多只能设置五种满折优惠");
                    } else {
                        T.ToastShort("最多只能设置五种满减优惠");
                    }
                    return;
                }
                Intent intent = new Intent(DiscountFullActivity.this, DiscountFullCreateActivity.class);
                String title = type.equals("3") ? "新建满减规则" : "新建满折规则";
                intent.putExtra("title", title);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        singleDiscount(type);
    }

    /**
     * 某个优惠折扣列表
     *
     * @param type 类型
     */
    private void singleDiscount(String type) {
        discountApi.setDialogHelper(mDialogHelper);
        discountApi.singleDiscount(type, new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                String ext = jsonData.getExt().toString();//开关
                discountView.getRightImageView().setSelected(ext.equals("1") ? true : false);
                discounts = GsonHelper.convertEntities(data,  Discount.class);
                DiscountAdapter adapter = new DiscountAdapter(DiscountFullActivity.this, discounts);
                adapter.setExt(ext);
                discountListView.setAdapter(adapter);

                if (ext.equals("2")) {
                    button.setEnabled(false);
                } else {
                    button.setEnabled(true);
                }
            }
        });
    }

    /**
     * 更新某个优惠折扣的开关接口
     */
    private void updateSwitch() {
        discountApi.setDialogHelper(null);
        discountApi.updateSwitch(type, new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                singleDiscount(type);
            }
        });
    }

    /**
     * 删除优惠折扣
     *
     * @param id
     */
    private void deleteDiscount(int id) {
        discountApi.setDialogHelper(null);
        discountApi.delete(String.valueOf(id), new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                T.ToastShort("删除成功");
                singleDiscount(type);
            }
        });
    }


    private class DiscountAdapter extends BaseAdapter {
        private String ext;
        private Context context;
        private List<Discount> discounts;

        private boolean isEnable() {
            return ext.equals("1");
        }

        public void setExt(String ext) {
            this.ext = ext;
        }

        public DiscountAdapter(Context context, List<Discount> discounts) {
            this.context = context;
            this.discounts = discounts;
        }

        @Override
        public int getCount() {
            return discounts.size();
        }

        @Override
        public Object getItem(int position) {
            return discounts.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_discount_full, null);
            TextView textView = (TextView) inflate.findViewById(R.id.tv_discount_name);

            final Discount discount = discounts.get(position);
            if (isEnable()) {
                textView.setEnabled(true);
                if (discount.getType() == 3) {
                    String exchange = getResources().getString(R.string.discount_3);
                    textView.setText(Html.fromHtml(String.format(exchange, PriceUtil.formatDouble(discount.getCondition()), PriceUtil.formatDouble(discount.getValue()))));
                } else {
                    String exchange = getResources().getString(R.string.discount_4);
                    textView.setText(Html.fromHtml(String.format(exchange, PriceUtil.formatDouble(discount.getCondition()), PriceUtil.formatDiscount(discount.getValue()))));
                }
            } else {
                textView.setEnabled(false);
                textView.setTextColor(Color.WHITE);
                if (discount.getType() == 3) {
                    textView.setText("满" + PriceUtil.formatDouble(discount.getCondition()) + "元立减" + PriceUtil.formatDouble(discount.getValue()) + "元");
                } else {
                    textView.setText("满" + PriceUtil.formatDouble(discount.getCondition()) + "元立享" + PriceUtil.formatDiscount(discount.getValue()) + "折");
                }
            }

            inflate.findViewById(R.id.iv_discount_delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteDiscount(discount.getId());
                }
            });
            if (!isEnable()) {
                inflate.findViewById(R.id.iv_discount_delete).setVisibility(View.GONE);
            }
            return inflate;
        }

    }

}
